package thread;

class Counter{
    private static int count = 0;

    synchronized public void add(){
        //synchronized (this) {
            count++;
        //}
    }

    public int get(){
        return count;
    }
}

public class Demo12 {
    //synchronized修饰方法

    public static void main(String[] args) throws InterruptedException {
        Counter count = new Counter();
        //Object locker = new Object();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                //synchronized (locker) {
                count.add();
                //}
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                //synchronized (locker) {
                count.add();
                //}
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(count.get());
    }
}
